<html>
<head>
    <title>cometd-yui3 test cases</title>
    <!--<script type="text/javascript" src="http://yui.yahooapis.com/3.0.0pr2/build/yui/yui-min.js"></script>-->
    <!--<script type="text/javascript" src="../cometd-yui3/cometd-yui3.js"></script>-->
    <script src="../cometd-yui3/cometd-yui3-loader.js" type="text/javascript"></script>
</head>
<body class="yui-skin-sam">
<h1>
    <script>document.write(document.title)</script>
</h1>
<script type="text/javascript">
comet.YUI({base:'../yui3/',cometBase:'../cometd-yui3/'}).use('yuitest', 'console', 'comet', function(Y) {

    Y.namespace('comet.tests')
    Y.comet.tests.Tests = new Y.Test.Case({
        name:'General Tests',
        setUp:function() {
            this.data = {
                key:'value'
            }
        },
        tearDown:function() {
            delete this.data;
            Y.Cookie.remove('BAYEUX_BROWSER', {path:'/'})
        },

        testConstructor:function() {
            Y.log('testConstructor', 'warn', 'test')
            var comet = new Y.Comet({});
            Y.Assert.isNotNull(comet);
            Y.Assert.isNotNull(comet._queue);//just to ensure the Y.Queue is correctly loaded
        },

        testCfg_SessionID:function() {
            Y.log('testCfg_SessionID', 'warn', 'test')
            Y.Assert.isFalse(new Y.Comet({extSessionId:false}).get('extSessionId'))
            Y.Assert.areEqual('sid', new Y.Comet({extSessionId:true}).get('extSessionId'))
            Y.Assert.areEqual('mySID', new Y.Comet({extSessionId:'mySID'}).get('extSessionId'))
        },

        testInitialState:function() {
            Y.log('testConstructor', 'warn', 'test')
            var comet = new Y.Comet({});
            Y.Assert.areEqual('unconnected', comet.getClientState())
            Y.Assert.isUndefined(comet.getClientId())
        },


        testPrepareHandshakeMessage:function() {
            Y.log('testPrepareHandshakeMessage', 'warn', 'test')
            var comet = new Y.Comet({ext:{all:{key0:'value0'}, handshake:{key1:'value1'}}});
            var msg = comet._createMessage('handshake', {ext:{key2:'value2'}});

            Y.Assert.areEqual('/meta/handshake', msg.channel)
            Y.Assert.areEqual('1.0', msg.version)

            //ext: all, handshake, message
            Y.Assert.isNotUndefined(msg.ext, 'msg.ext is undefined')
            Y.Assert.isNotUndefined(msg.ext.key0, 'msg.ext.key0 is undefined')
            Y.Assert.areEqual('value0', msg.ext.key0, 'msg.ext.key0 is invalid')
            Y.Assert.isNotUndefined(msg.ext.key1, 'msg.ext.key1 is undefined')
            Y.Assert.areEqual('value1', msg.ext.key1, 'msg.ext.key1 is invalid')
            Y.Assert.isNotUndefined(msg.ext.key2, 'msg.ext.key2 is undefined')
            Y.Assert.areEqual('value2', msg.ext.key2, 'msg.ext.key2 is invalid')
        },

        testHandshake_Successful:function() {
            Y.log('testHandshake_Successful', 'warn', 'test')
            var comet = new Y.Comet({uri:'.cometd'});
            Y.Assert.areEqual('unconnected', comet.getClientState())
            Y.Assert.isUndefined(comet.getClientId());

            // case 1: successful handshake
            comet.on('handshake', function(r) {
                this.resume(function() {
                    Y.Assert.areEqual('connecting', comet.getClientState())
                    Y.Assert.isNotUndefined(comet.getClientId());
                    Y.log('clientID: ' + comet.getClientId())

                    var requiredRespFields = ['channel', 'version', 'supportedConnectionTypes', 'clientId', 'successful']
                    Y.each(requiredRespFields, function(p) {
                        Y.Assert.isNotUndefined(r[p], 'response property \'' + p + '\' is undefined')
                    })
                    Y.Assert.isTrue(r.successful)
                    //Y.log('response: ' + Y.JSON.stringify(resp))

                    this.wait(function(){ comet.disconnect(); },1000)
                })
            }, this);
            comet.handshake();
            this.wait();
        },

        //TODO consider to add a handshake failure case, if it is ever possible to create such scenario

        testHandshake_Error:function() {
            Y.log('testHandshake_Error', 'warn', 'test')
            var comet = new Y.Comet({uri:'.invalid'});

            comet.on('error', function(o) {
                this.resume(function() {
                    Y.Assert.areEqual('unconnected', comet.getClientState())
                    Y.Assert.isUndefined(comet.getClientId());
                    //Y.log('o: ' + Y.JSON.stringify(o))
                })
            }, this);
            comet.handshake();
            this.wait(1000);

            Y.Assert.areEqual('unconnected', comet.getClientState())
        },

        testDirectConnect:function() {
            Y.log('testDirectConnect', 'warn', 'test')
            var comet = new Y.Comet({uri:'.cometd'});
            Y.Assert.areEqual('unconnected', comet.getClientState())
            Y.Assert.isUndefined(comet.getClientId());

            comet.on('handshake', function(r) {
                this.resume(Y.bind(function() {
                    Y.Assert.isNotUndefined(comet.getClientId())
                    this.wait(function() { Y.Assert.fail('fail to trigger the \'connect\' event with 1000ms')}, 1000);
                }, this))
            }, this)

            comet.on('connect', function() {
                this.resume(function() {
                    Y.Assert.areEqual('connected', comet.getClientState())

                    this.wait(function() {
                        comet.disconnect();
                    }, 1000);
                })
            }, this)
            comet.connect();
            this.wait(function() { Y.Assert.fail('fail to trigger the \'handshake\' event with 1000ms')}, 1000);
        },

        testConnectDisconnect:function() {
            Y.log('testConnectDisconnect', 'warn', 'test')
            var comet = new Y.Comet({uri:'.cometd'});
            Y.Assert.areEqual('unconnected', comet.getClientState())
            Y.Assert.isUndefined(comet.getClientId());

            // first receive a 'connect' event after making the connection
            comet.on('connect', function() {
                Y.log('testConnectDisconnect() - on \'connect\'', 'debug', 'cometd-yui3-test')
                this.resume(function() {
                    Y.Assert.areEqual('connected', comet.getClientState())
                    comet.disconnect()
                    this.wait(function() { Y.Assert.fail('fail to trigger the \'disconnect\' event with 1000ms')}, 1000);
                })
            }, this)

            comet.on('disconnect', function(r) {
                Y.log('testConnectDisconnect() - on \'disconnect\'', 'debug', 'cometd-yui3-test')
                this.resume(function() {
                    Y.Assert.areEqual('unconnected', comet.getClientState())
                    Y.Assert.isUndefined(comet.getClientId())
                    Y.Assert.isTrue(r.successful)
                    Y.Assert.areEqual('/meta/disconnect', r.channel)
                })
            }, this)
            comet.connect();
            this.wait(function() { Y.Assert.fail('fail to trigger the \'connect\' event with 1000ms')}, 1000);
        },

        /**
         * The 2nd handshake shall force a disconnect before performing the handshake
         */
        testDoubleHandshake_disconnectOnReconnect_true:function() {
            Y.log('testDoubleHandshake_disconnectOnReconnect_true', 'warn', 'test')
            var comet = new Y.Comet({uri:'.cometd'});
            comet.handshake();
            var clientId = comet.getClientId()
            this.wait(function() {
                Y.Assert.areEqual('connected', comet.getClientState())
                comet.handshake();

                this.wait(function() {
                    Y.Assert.areNotEqual(clientId, comet.getClientId())
                    comet.disconnect();
                }, 1000)
            }, 1000)
        },
        XLastFunction:function() {
        }
    })
    //TODO add test cases about handleAdvice

    Y.comet.tests.DevTests = new Y.Test.Case({
        name: 'Development Tests',
        setUp:function() {
            this.data = {
            }
        },
        tearDown:function() {
            delete this.data;
        },
          
        XLastFunction:function() {
        }
    })

    //TODO: https://sourceforge.net/tracker2/?func=detail&atid=836476&aid=2431381&group_id=165715
    var console = new Y.Console()
    console.get('contentBox').setStyle('width', (Y.get('body').get('docWidth') * 3 / 4) + 'px')
    console.render()
    console.get('contentBox').query('div .yui-console-bd').setStyle('height', (Y.get('body').get('docHeight') * 4 / 5) + 'px')
    console.on('entry', function(e) {
        if (e.message.source !== 'TestRunner') {
            e.preventDefault()
        }
    })

    Y.Test.Runner.add(Y.comet.tests.DevTests);
    if (!Y.Comet.any(Y.comet.tests.DevTests, function(v, k) {
        return /^test/.test(k)
    })) {
        // if there are any development tests, normal test won't run
        Y.Test.Runner.add(Y.comet.tests.Tests);
    }
    Y.Test.Runner.run();
})
</script>


</body>
</html>